<?php

class MtsysBaseController extends RController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
    public $defaultAction = 'admin';
	public $layout='//layouts/lay_column2';
    public $menu=array();
    public $moduleName="站点";	//此处修改本模块名称
    
    public function init()
	{
    	$leftmenu = 'yunying';	//此处修改模块所属菜单
		$this->menu = Yii::app()->params[$leftmenu];
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','getCitys','getCountys'),
				'users'=>array('@'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','create1'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate($p,$c,$a)
	{
		$model=new MtsysBase;
		$user = User::model()->findByPk(Yii::app()->user->id);
		
/*		if($user->unit_id != $a && !Yii::app()->getModule('user')->isAdmin()){
			echo '您没有权限，只有区县级可以新建站点';
			exit;	
		}*/
		
		if(isset($_POST['MtsysBase']))
		{
			$model->attributes=$_POST['MtsysBase'];
			//$model->BASE_TYPE = $_POST['MtsysBaseType']['RECORD_ID'];
			if($model->save()){				
				//$this->redirect(array('view','id'=>$model->BASE_ID));
				echo '<script>parent.window.baseDialogClose();</script>';
				exit;
			}
		}

		$province = MtsysProvince::model()->findByPk($p);
		$city = MtsysCity::model()->findByPk($c);
		$county = MtsysCounty::model()->findByPk($a);
//		$base_types = MtsysBasetype::model()->findAll();
		$base_types = Btstype::model()->findAll();
		
		$this->renderPartial('create',array(
			'model'=>$model,			
			'province'=>$province, 
			'city'=>$city,
			'county'=>$county,
			'base_types'=>$base_types,
		));
	}

	public function actionCreate1($p,$c,$a,$id)
	{
		//$model=new MtsysBase;
		$model=$this->loadModel($id);
		$user = User::model()->findByPk(Yii::app()->user->id);
	
		/*		if($user->unit_id != $a && !Yii::app()->getModule('user')->isAdmin()){
		 echo '您没有权限，只有区县级可以新建站点';
		exit;
		}*/
	
		if(isset($_POST['MtsysBase']))
		{
			$model->attributes=$_POST['MtsysBase'];
			//$model->BASE_TYPE = $_POST['MtsysBaseType']['RECORD_ID'];
			$model->BASE_TYPE = $_POST['MtsysBaseType']['RECORD_ID'];
			if($model->save()){
				//$this->redirect(array('view','id'=>$model->BASE_ID));
				echo '<script>parent.window.baseDialogClose();</script>';
				exit;
			}
		}
	
		$province = MtsysProvince::model()->findByPk($p);
		$city = MtsysCity::model()->findByPk($c);
		$county = MtsysCounty::model()->findByPk($a);
		//		$base_types = MtsysBasetype::model()->findAll();
		$base_types = Btstype::model()->findAll();
	
		$this->renderPartial('_update',array(
				'model'=>$model,
				'province'=>$province,
				'city'=>$city,
				'county'=>$county,
				'base_types'=>$base_types,
		));
	}	
	
	public function actionGetCitys()
	{
		$rs = '';
		$data = MtsysCity::model()->findAll('PROVINCE_ID=:PID',array(':PID'=>$_POST['PROVINCE_ID']));
        $data=CHtml::listData($data,'CITY_ID','CITY_NAME');
		
        $rs .= CHtml::tag('option',array('value'=>'0'),CHtml::encode('请选择市级'),true);
        
        if(!empty($data)){
			foreach($data as $value=>$name){
        		$rs .= CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
        	}
		}
			
		echo $rs;
	}
	
	public function actionGetCountys()
	{
		$rs = '';
		
		$data=MtsysCounty::model()->findAll('CITY_ID=:CID',
                      array(':CID'=>(int)$_POST['CITY_ID']));
    
		$rs .= CHtml::tag('option',array('value'=>'0'),CHtml::encode('请选择区县级'),true);
        $data=CHtml::listData($data,'COUNTY_ID','COUNTY_NAME');
        if(!empty($data)){
			foreach($data as $value=>$name){
        		$rs .= CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
        	}
		}
		
		echo $rs;
	}
	
	public function actionGetBases()
	{
		$rs = '';
		$data=MtsysBase::model()->findAll('COUNTY_ID=:CID',
                      array(':CID'=>(int)$_POST['COUNTY_ID']));

		$rs .= CHtml::tag('option',array('value'=>'0'),CHtml::encode('请选择站点'),true);
        $data=CHtml::listData($data,'BASE_ID','BASE_NAME');     
        if(!empty($data)){
			foreach($data as $value=>$name){
        		$rs .= CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
        	}
		}
		echo $rs;
	}
	
	public function actionGetBasesCk()
	{
		$rs = '';
		$data=MtsysBase::model()->findAll('COUNTY_ID=:CID',
                      array(':CID'=>(int)$_POST['COUNTY_ID']));
    
        $data=CHtml::listData($data,'BASE_ID','BASE_NAME');
        if(!empty($data)){
			foreach($data as $value=>$name){
        		//echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
				$rs .= '<input id="User_BASE_ID_'.$value.'" value="'.$value.'" type="checkbox" name="User[BASE_ID][]">';
				$rs .= '<label for="User_BASE_ID_'.$value.'">'.$name.'</label></span>';
        	}
		}
		echo $rs;
	}
	
	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['MtsysBase']))
		{
			$model->attributes=$_POST['MtsysBase'];
			$model->BASE_TYPE = $_POST['MtsysBaseType']['RECORD_ID'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->BASE_ID));
		}
		
		$mUser=User::model()->findByPk(Yii::app()->user->id);
		$user_unit_id = $mUser->unit_id;
		$user_unit_level = $mUser->unit_level;
		
		/*
		$provinces = NULL;
		$citys = NULL;
		$countys = NULL;
		
		switch($user_unit_level){
			case 0:	//chaoji
				$provinces = MtsysProvince::model()->findAll();
				break;
			case 1:
				$provinces = MtsysProvince::model()->find('PROVINCE_ID=:PID',array(':PID'=>$unit_id));	
				break;
			case 2:
				$citys = MtsysCity::model()->find('CITY_ID=:CID',array(':CID'=>$unit_id));
				$provinces = MtsysProvince::model()->findAll('PROVINCE_ID=:PID',array(':PID'=>$citys->PROVINCE_ID));
				break;
			case 3:
				$county = MtsysCounty::model()->find('COUNTY_ID=:CID',array(':CID'=>$unit_id));
				$citys = MtsysCity::model()->find('CITY_ID=:CID',array(':CID'=>$county->CITY_ID));	
			$provinces = MtsysProvince::model()->find('PROVINCE_ID=:PID',array(':PID'=>$citys->PROVINCE_ID));		
		}
		*/
		
		$provinces = MtsysProvince::model()->findAll();
		$citys = MtsysCity::model()->findAll();
		$countys = MtsysCounty::model()->findAll();
		
		$base_types = Btstype::model()->findAll();
		$this->render('update',array(
			'model'=>$model,
			'provinces' => $provinces,
			'citys' => $citys,
			'countys' => $countys,
			'base_types' => $base_types,
			'level' => $user_unit_level,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('MtsysBase');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$user = User::model()->findByPk(Yii::app()->user->id);
		
		$model=new MtsysBase('search');
		
		if(Yii::app()->getModule('user')->isAdmin()){
			$model->unsetAttributes();  // clear any default values				
		}else{			
			switch($user->unit_level){
				case 1:
					$model->PROVINCE_ID=$user->unit_id;
					break;
				case 2:
					$model->CITY_ID=$user->unit_id; 
					break;
				case 3:
					$model->COUNTY_ID=$user->unit_id; 
					break;
			}
		}
		
		if(isset($_GET['MtsysBase']))
				$model->attributes=$_GET['MtsysBase'];	
		
		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=MtsysBase::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='mtsys-base-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
